1 ===============================================================================
2 MICROSOFT FOUNDATION CLASS LIBRARY : MFCCOMClient Project Overview
3 ===============================================================================
5 /////////////////////////////////////////////////////////////////////////////
8 As we discussed in the sample CppCOMClient, there are basically three ways to
9 create and access a COM object in a native client. MFC is one of them. This
10 sample demonstrates the following two things:
12 1. The creation and access of a COM object (more specifically, an ATL out-of-
13 process COM object in this sample) using MFC and the class wrapper generated
14 by the VC++ class wizard.
16 CATLExeSimpleObjectWrapper.h contains the wrapper class of the COM server;
17 MFCCreateCOMPage.h/cpp has the codes that use the wrapper class to create and
18 access a COM server ATLExeCOMServer; IDD_CREATECOM_PAGE is the property page
19 in the dialog that demonstrates the COM client.
21 2. The creation and access of an ActiveX control using MFC to add the control
22 to the dialog template.
24 mfcactivexctrl.h/cpp has the wrapper class of the ActiveX control that is
25 inserted into the dialog IDD_ACTIVEXCTRL_PAGE. MFCActiveXCtrlPage.h/cpp
26 corresponds to the dialog resource.
29 /////////////////////////////////////////////////////////////////////////////
32 MFCCOMClient -> ATLExeCOMServer
33 MFCCOMClient is the client application of the COM server ATLExeCOMServer.
35 MFCCOMClient -> MFCActiveX
36 MFCCOMClient hosts the ActiveX control MFCActiveX.
39 /////////////////////////////////////////////////////////////////////////////
42 MFCCOMClient depends on ATLExeCOMServer and MFCActiveX. To build and run
43 MFCCOMClient successfully, please make sure ATLExeCOMServer and MFCActiveX
44 are built and registered rightly.
47 /////////////////////////////////////////////////////////////////////////////
50 A. Creating the project
52 Step1. Create a MFC dialog-based project named MFCCOMClient.
54 Step2. Build the UI of the demo.
56 2.1 Add a Tab Control to the main dialog and add a CTabCtrl member,
57 m_tabCtrl, to be bound to the control.
59 2.2 Insert two dialog resources IDD_CREATECOM_PAGE, IDD_ACTIVEXCTRL_PAGE and
60 set their Style as Child, Border as None. Use the class wizard to create two
61 classes, CMFCCreateCOMPage and CMFCActiveXCtrlPage for the dialog resources
64 2.3 In the OnInitDialog method of the main dialog class CMFCCOMClientDlg,
65 setup the tab control, m_tabCtrl, by inserting two tab items and show
66 IDD_CREATECOM_PAGE and IDD_ACTIVEXCTRL_PAGE in them.
68 B. Creating a COM object using MFC (MFCCreateCOMPage.h/cpp)
70 Step1. Create the wrapper class to encapsulate the COM class.
72 1.1 Right-click the MFCCOMClient project and select Add / Add Class. Double-
73 click "MFC Class From TypeLib".
75 1.2 Find the type library of ATLExeCOMServer in Add Class From Typelib
76 Wizard, and add the interface ISimpleObject from the Interfaces column
77 to the column of Generated classes. Rename the resulting class and file as
78 CATLExeSimpleObjectWrapper, then click Finish.
80 This creates the file CATLExeSimpleObjectWrapper.h with the class
81 CATLExeSimpleObjectWrapper that encapsulate the COM interface
82 ATLExeCOMServer.ISimpleObject.
84 Step2. Create and access the COM object using the wrapper class.
86 2.1 In the class CMFCCreateCOMPage, create a new thread (AfxBeginThread) and
87 initialize COM for the thread by calling CoInitializeEx, or CoInitialize.
89 2.2 Create an instance of the wrapper class CATLExeSimpleObjectWrapper and
90 use its CreateDispatch() method to create the actual COM object.
92 2.3 Access the COM object's methods and properties. In order to catch the
93 errors in the execution, add a try {...} catch (COleException *e) {} block.
95 2.4 The wrapper class handles the release of the object for us, so we do not
96 need to care about it.
98 2.5 Uninitialize COM for this thread by calling CoUninitialize.
100 C. Hosting an ActiveX control (MFCActiveXCtrlPage.h/cpp)
102 Step1. On the dialog IDD_ACTIVEXCTRL_PAGE in which we will add the ActiveX
103 control, right-click the mouse and select Insert ActiveX Control.
105 Step2. Find "MFCActiveX Control" in the Insert ActiveX Control dialog and add
106 it. A new control appears on IDD_ACTIVEXCTRL_PAGE. Stretch and position the
107 control appropriately.
109 Step3. Right-click the control, and select Add Variable. In Add Member
110 Variable Wizard, set Variable type as CMFCActiveXCtrl, enter m_OcxActiveXCtrl
111 in Variable name and click Finish. This generates the class CMFCActiveXCtrl
112 that wraps the ActiveX control in mfcactivexctrl.h/cpp, and adds a variable,
113 CMFCActiveXCtrl m_OcxActiveXCtrl, to the class CMFCActiveXCtrlPage.
115 Step4. Access the properties and methods of the ActiveX control like this:
117 FLOAT fProp = m_OcxActiveXCtrl.GetFloatProperty();
119 Step5. To access the events of the control (e.g. FloatPropertyChanging in
120 MFCActiveX), right-click the ActiveX control on IDD_ACTIVEXCTRL_PAGE and
121 select Add Event Handler. Rename the function handler of
122 FloatPropertyChanging to be FloatPropertyChangingMFCActiveXCtrl and click
123 Add and Edit. In the event handler, pop up a message box to allow selecting
124 OK or Cancel, then pass the user's selection back to the control through the
127 void CMFCActiveXCtrlPage::FloatPropertyChangingMFCActiveXCtrl(
128 float NewValue, BOOL* Cancel)
132 _T("FloatProperty is being changed to %f"), NewValue);
134 // OK or cancel the change of FloatProperty
135 *Cancel = (IDCANCEL == MessageBox(strMessage,
136 _T("MFCActiveX!FloatPropertyChanging"), MB_OKCANCEL));
140 /////////////////////////////////////////////////////////////////////////////
143 COM Programming by Example: Using MFC, ActiveX, ATL, ADO, and COM+
145 http://www.amazon.com/COM-Programming-Example-ActiveX-CD-ROM/dp/1929629036
147 Viewing and Adding ActiveX Controls to a Dialog Box
148 http://msdn.microsoft.com/en-us/library/6e37cay9.aspx
150 Using MFC to Host a WebBrowser Control
151 http://msdn.microsoft.com/en-us/library/aa752046.aspx
153 Using ActiveX Controls Example: Insert Internet Explorer into your Dialogs
155 http://www.codeproject.com/KB/COM/webbrowser.aspx
158 /////////////////////////////////////////////////////////////////////////////